package cn.com.darly.videoandroidpm.ui.artamuse;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

import com.darly.common.ToastApp;
import com.darly.dview.framework.ContentBinder;
import com.darly.dview.framework.ViewsBinder;
import com.darly.dview.widget.header.TitleView;

import java.util.LinkedHashMap;
import java.util.Map;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.ui.biz.NewAmuseListener.NewAmuseInterface;
import cn.com.darly.videoandroidpm.ui.presenter.NewAmusePresenter;
import cn.com.darly.videoandroidpm.widget.richeditor.RichEditor;
import cn.com.darly.videoandroidpm.widget.richeditor.RichEditor.OnTextChangeListener;

/**
 * @author Darly/张宇辉/2018/5/11 14:36
 * @version 1.0/cn.com.darly.videoandroidpm.ui.artamuse
 */
@ContentBinder(R.layout.activity_new_amuse)
public class NewAmuseActivity extends BaseActivity implements OnTextChangeListener,OnClickListener ,NewAmuseInterface{
    @ViewsBinder(R.id.id_base_title)
    public TitleView id_base_title;



    @ViewsBinder(R.id.id_new_amuse_title)
    EditText id_new_amuse_title;
    @ViewsBinder(R.id.id_new_amuse_richedit)
    RichEditor id_new_amuse_richedit;

    @ViewsBinder(R.id.id_new_amuse_action_undo)
    ImageButton id_new_amuse_action_undo;
    @ViewsBinder(R.id.id_new_amuse_action_redo)
    ImageButton id_new_amuse_action_redo;
    @ViewsBinder(R.id.id_new_amuse_action_bold)
    ImageButton id_new_amuse_action_bold;
    @ViewsBinder(R.id.id_new_amuse_action_italic)
    ImageButton id_new_amuse_action_italic;
    @ViewsBinder(R.id.id_new_amuse_action_subscript)
    ImageButton id_new_amuse_action_subscript;
    @ViewsBinder(R.id.id_new_amuse_action_superscript)
    ImageButton id_new_amuse_action_superscript;
    @ViewsBinder(R.id.id_new_amuse_action_strikethrough)
    ImageButton id_new_amuse_action_strikethrough;
    @ViewsBinder(R.id.id_new_amuse_action_underline)
    ImageButton id_new_amuse_action_underline;
    @ViewsBinder(R.id.id_new_amuse_action_heading1)
    ImageButton id_new_amuse_action_heading1;
    @ViewsBinder(R.id.id_new_amuse_action_heading2)
    ImageButton id_new_amuse_action_heading2;
    @ViewsBinder(R.id.id_new_amuse_action_heading3)
    ImageButton id_new_amuse_action_heading3;
    @ViewsBinder(R.id.id_new_amuse_action_heading4)
    ImageButton id_new_amuse_action_heading4;
    @ViewsBinder(R.id.id_new_amuse_action_heading5)
    ImageButton id_new_amuse_action_heading5;
    @ViewsBinder(R.id.id_new_amuse_action_heading6)
    ImageButton id_new_amuse_action_heading6;
    @ViewsBinder(R.id.id_new_amuse_action_txt_color)
    ImageButton id_new_amuse_action_txt_color;
    @ViewsBinder(R.id.id_new_amuse_action_bg_color)
    ImageButton id_new_amuse_action_bg_color;
    @ViewsBinder(R.id.id_new_amuse_action_indent)
    ImageButton id_new_amuse_action_indent;
    @ViewsBinder(R.id.id_new_amuse_action_outdent)
    ImageButton id_new_amuse_action_outdent;
    @ViewsBinder(R.id.id_new_amuse_action_align_left)
    ImageButton id_new_amuse_action_align_left;
    @ViewsBinder(R.id.id_new_amuse_action_align_center)
    ImageButton id_new_amuse_action_align_center;
    @ViewsBinder(R.id.id_new_amuse_action_align_right)
    ImageButton id_new_amuse_action_align_right;
    @ViewsBinder(R.id.id_new_amuse_action_insert_bullets)
    ImageButton id_new_amuse_action_insert_bullets;
    @ViewsBinder(R.id.id_new_amuse_action_insert_numbers)
    ImageButton id_new_amuse_action_insert_numbers;
    @ViewsBinder(R.id.id_new_amuse_action_blockquote)
    ImageButton id_new_amuse_action_blockquote;
    @ViewsBinder(R.id.id_new_amuse_action_insert_image)
    ImageButton id_new_amuse_action_insert_image;
    @ViewsBinder(R.id.id_new_amuse_action_insert_link)
    ImageButton id_new_amuse_action_insert_link;
    @ViewsBinder(R.id.id_new_amuse_action_insert_checkbox)
    ImageButton id_new_amuse_action_insert_checkbox;

    private boolean isChanged;
    private boolean isChanger;

    private String builder;

    private NewAmusePresenter presenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        id_base_title.setTitle("新增话题");

        id_new_amuse_richedit.setEditorHeight(200);
        id_new_amuse_richedit.setEditorFontSize(22);
        id_new_amuse_richedit.setEditorFontColor(Color.RED);
        //mEditor.setEditorBackgroundColor(Color.BLUE);
        //mEditor.setBackgroundColor(Color.BLUE);
        //mEditor.setBackgroundResource(R.drawable.bg);
        id_new_amuse_richedit.setPadding(10, 10, 10, 10);
        //mEditor.setBackground("https://raw.githubusercontent.com/wasabeef/art/master/chip.jpg");
        id_new_amuse_richedit.setPlaceholder("请在这里输入话题内容");
        //mEditor.setInputEnabled(false);
    }

    @Override
    protected void loadData() {
        presenter = new NewAmusePresenter(this);
    }

    @Override
    protected void initListener() {
        id_base_title.setLeftBackOneListener(R.mipmap.ic_title_back,this);
        id_base_title.setRightViewRightTextOneListener("提交审核",this);
        id_new_amuse_richedit.setOnTextChangeListener(this);

        id_new_amuse_action_undo.setOnClickListener(this);
        id_new_amuse_action_redo.setOnClickListener(this);
        id_new_amuse_action_bold.setOnClickListener(this);
        id_new_amuse_action_italic.setOnClickListener(this);
        id_new_amuse_action_subscript.setOnClickListener(this);
        id_new_amuse_action_superscript.setOnClickListener(this);
        id_new_amuse_action_strikethrough.setOnClickListener(this);
        id_new_amuse_action_underline.setOnClickListener(this);
        id_new_amuse_action_heading1.setOnClickListener(this);
        id_new_amuse_action_heading2.setOnClickListener(this);
        id_new_amuse_action_heading3.setOnClickListener(this);
        id_new_amuse_action_heading4.setOnClickListener(this);
        id_new_amuse_action_heading5.setOnClickListener(this);
        id_new_amuse_action_heading6.setOnClickListener(this);
        id_new_amuse_action_txt_color.setOnClickListener(this);
        id_new_amuse_action_bg_color.setOnClickListener(this);
        id_new_amuse_action_indent.setOnClickListener(this);
        id_new_amuse_action_outdent.setOnClickListener(this);
        id_new_amuse_action_align_left.setOnClickListener(this);
        id_new_amuse_action_align_center.setOnClickListener(this);
        id_new_amuse_action_align_right.setOnClickListener(this);
        id_new_amuse_action_insert_bullets.setOnClickListener(this);
        id_new_amuse_action_insert_numbers.setOnClickListener(this);
        id_new_amuse_action_blockquote.setOnClickListener(this);
        id_new_amuse_action_insert_image.setOnClickListener(this);
        id_new_amuse_action_insert_link.setOnClickListener(this);
        id_new_amuse_action_insert_checkbox.setOnClickListener(this);
    }

    @Override
    public void onTextChange(String text) {
        builder = text;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_view_ok:
                if (TextUtils.isEmpty(builder)){
                    ToastApp.showToast("发布信息不能为空");
                }else {
                    String title = id_new_amuse_title.getText().toString().trim();
                    Map<String, String> params = new LinkedHashMap<String, String>();
                    params.put("title", TextUtils.isEmpty(title)?"暂无标题":title);
                    params.put("content", builder.trim());
                    presenter.onPublish(params);
                }
                break;
            case R.id.title_view_back_img:
                finish();
                break;
            case R.id.id_new_amuse_action_undo:
                id_new_amuse_richedit.undo();
                break;
            case R.id.id_new_amuse_action_redo:
                id_new_amuse_richedit.redo();
                break;
            case R.id.id_new_amuse_action_bold:
                id_new_amuse_richedit.setBold();
                break;
            case R.id.id_new_amuse_action_italic:
                id_new_amuse_richedit.setItalic();
                break;
            case R.id.id_new_amuse_action_subscript:
                id_new_amuse_richedit.setSubscript();
                break;
            case R.id.id_new_amuse_action_superscript:
                id_new_amuse_richedit.setSuperscript();
                break;
            case R.id.id_new_amuse_action_strikethrough:
                id_new_amuse_richedit.setStrikeThrough();
                break;
            case R.id.id_new_amuse_action_underline:
                id_new_amuse_richedit.setUnderline();
                break;
            case R.id.id_new_amuse_action_heading1:
                id_new_amuse_richedit.setHeading(1);
                break;
            case R.id.id_new_amuse_action_heading2:
                id_new_amuse_richedit.setHeading(2);
                break;
            case R.id.id_new_amuse_action_heading3:
                id_new_amuse_richedit.setHeading(3);
                break;
            case R.id.id_new_amuse_action_heading4:
                id_new_amuse_richedit.setHeading(4);
                break;
            case R.id.id_new_amuse_action_heading5:
                id_new_amuse_richedit.setHeading(5);
                break;
            case R.id.id_new_amuse_action_heading6:
                id_new_amuse_richedit.setHeading(6);
                break;
            case R.id.id_new_amuse_action_txt_color:
                id_new_amuse_richedit.setTextColor(isChanged ? Color.BLACK : Color.RED);
                isChanged = !isChanged;
                break;
            case R.id.id_new_amuse_action_bg_color:
                id_new_amuse_richedit.setTextBackgroundColor(isChanger ? Color.TRANSPARENT : Color.YELLOW);
                isChanger = !isChanger;
                break;
            case R.id.id_new_amuse_action_indent:
                id_new_amuse_richedit.setIndent();
                break;
            case R.id.id_new_amuse_action_outdent:
                id_new_amuse_richedit.setOutdent();
                break;
            case R.id.id_new_amuse_action_align_left:
                id_new_amuse_richedit.setAlignLeft();
                break;
            case R.id.id_new_amuse_action_align_center:
                id_new_amuse_richedit.setAlignCenter();
                break;
            case R.id.id_new_amuse_action_align_right:
                id_new_amuse_richedit.setAlignRight();
                break;
            case R.id.id_new_amuse_action_insert_bullets:
                id_new_amuse_richedit.setBullets();
                break;
            case R.id.id_new_amuse_action_insert_numbers:
                id_new_amuse_richedit.setNumbers();
                break;
            case R.id.id_new_amuse_action_blockquote:
                id_new_amuse_richedit.setBlockquote();
                break;
            case R.id.id_new_amuse_action_insert_image:
                id_new_amuse_richedit.insertImage("http://www.1honeywan.com/dachshund/image/7.21/7.21_3_thumb.JPG",
                        "dachshund");
                break;
            case R.id.id_new_amuse_action_insert_link:
                id_new_amuse_richedit.insertLink("https://github.com/wasabeef", "wasabeef");
                break;
            case R.id.id_new_amuse_action_insert_checkbox:
                id_new_amuse_richedit.insertTodo();
                break;
        }
    }

    @Override
    public void onNewAmuseSuccess(Object code) {
        ToastApp.showToast("已经发布成功，请等待审核");
        finish();
    }

    @Override
    public void onNewAmuseFailed(String info) {
        ToastApp.showToast(info);
    }

    @Override
    public void setDisableClick() {

    }

    @Override
    public void setEnableClick() {

    }
}
